package com.dmwork.service.interaction.controller.front;

import com.dmwork.common.base.result.R;
import com.dmwork.service.interaction.entity.CmsCourse;
import com.dmwork.service.interaction.entity.vo.CmsCourseVo;
import com.dmwork.service.interaction.service.CmsCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/front/dm_work/interaction/cms_course")
public class FrontCmsCourseController {

    @Autowired
    private CmsCourseService cmsCourseService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "course_list";

    @GetMapping("get_course_list")
    public R<List<CmsCourseVo>> getCourseList() {
        List<CmsCourseVo> cmsCourseVoList = null;
        Boolean hasKey = redisTemplate.hasKey(KEY);
        if (hasKey != null && hasKey) {
            cmsCourseVoList = (List<CmsCourseVo>) redisTemplate.opsForValue().get(KEY);
        } else {
            cmsCourseVoList = cmsCourseService.getCourseList();
            if (cmsCourseVoList != null && cmsCourseVoList.size() > 0)
                redisTemplate.opsForValue().set(KEY, cmsCourseVoList);
        }
        return R.ok("items", cmsCourseVoList);
    }

}
